<?php
$script = "
jQuery('.actions a.revert').click(function(){
    if (".($this->get('layout' , '') == 'popup' ? 'true' : 'false')." && parent.jQuery.confirm_discard_changed_page() == false)
        return false;
    var url = jQuery(this).attr('href');
    var revision = jQuery('.revisions select').val();
    url = jQuery.param.querystring(url, {'revision' : revision});
    jQuery.get(url, jQuery.proxy(function(data){
        data = eval(data);
        if (data.status != undefined && data.status)
        {
            if (".($this->get('layout', '') === 'popup' ? 'true' : 'false').")
                parent.window.location = parent.window.location;
        }
    }, this));
    return false;
});

var current = jQuery('.revisions select').val();
jQuery.recheck = function(){
    if (jQuery('.revisions select option').size() <= 1)
        jQuery('.actions').hide();
    else
        jQuery('.actions').show();
    if (jQuery('.revisions select').val() == current)
        jQuery('.actions .revert').hide();
    else
        jQuery('.actions .revert').show();
};
jQuery.recheck();

jQuery('.revisions select').change(function(){
    jQuery.recheck();
});
";
Yii::app()->clientScript->registerCoreScript('bbq');
Yii::app()->clientScript->registerScript('RevertPage', $script, CClientScript::POS_READY);
Yii::app()->clientScript->registerScriptFile(Yii::app()->theme->baseUrl.'/scripts/fancybox/jquery.fancybox-1.3.4.pack.js');
Yii::app()->clientScript->registerCssFile(Yii::app()->theme->baseUrl.'/scripts/fancybox/jquery.fancybox-1.3.4.css');

$script = "
jQuery('.actions a.preview').click(function(){
    var url = jQuery(this).attr('href');
    var revision = jQuery('.revisions select').val();
    url = jQuery.param.querystring(url, {'preview' : 1, 'revision' : revision});
    jQuery.fancybox({
        'titleShow'         : false,
        'height'            : '100%',
        'width'             : '100%',
        'autoScale'         : true,
        'transitionIn'      : 'none',
        'transitionOut'     : 'none',
        'type'              : 'iframe',
        'href'              : url,
        'onComplete'        : function(){
            var timer = setInterval(function(){
                var iframe = jQuery('#fancybox-frame').contents();
                var body = iframe.find('body');
                var toolbar = iframe.find('#sb-page-controls');
                if (body.size() && toolbar.size())
                {
                    body.css('cssText', 'margin-top: 0 !important');
                    toolbar.hide();
                    clearInterval(timer);
                }
            }, 500);
        }
    });
    return false;
});
";
Yii::app()->clientScript->registerScript(__CLASS__.'#Preview', $script, CClientScript::POS_READY);

$url = is_object($model->defaultUrl) ? Yii::app()->request->hostInfo.Yii::app()->request->baseUrl.'/'.ltrim($model->defaultUrl->url, '/') : '#';
?>
<style type="text/css">
.actions {padding-left: 210px;}
.actions a {margin-right: 10px;}
.revisions label {
    color: #052B54;
    display: block;
    font-size: 0.9em;
    font-weight: bold;
    float: left;
    line-height: 24px;
    margin-right: 10px;
    position: relative;
    text-align: right;
    width: 200px;
}
.revisions select {margin: 0.2em 0 0.5em;}
</style>

<div class="revisions">
    <?php echo CHtml::label('Revision', 'revision');?>
    <?php $this->widget('Cms.widgets.PageRevisionDropDownList', array('name'=>'revision', 'pageId'=>$model->id));?>
</div>
<div class="actions">
    <?php echo CHtml::link('Revert', array('/Core/service/ajax', 'SID'=>'Cms.Page.revert', 'page_id'=>$model->id), array('class'=>'revert'))?>
    <?php echo CHtml::link('Preview', $url, array('class'=>'preview'))?>
</div>

